--CREATE FUNCTION [tsql].[fn_levensthein]
DECLARE @str_ref NVARCHAR(4000)
	  , @str_cmp NVARCHAR(4000)
 
	
SET @str_ref = 'EDWARD'
SET @str_cmp = 'EDUARDO' 

BEGIN  

DECLARE @len_ref INT
DECLARE @len_cmp INT  
DECLARE @idx_ref INT
DECLARE @idx_cmp INT
DECLARE @idx_tmp INT  
DECLARE @chr_ref NCHAR(1)
DECLARE @chr_cmp NCHAR(1)
DECLARE @bin_ref VARBINARY(8000)
DECLARE @bin_cmp VARBINARY(8000)  
DECLARE @lev_dist INT


SET @len_ref = LEN(@str_ref)
SET @idx_ref = 1
SET @len_cmp = LEN(@str_cmp)
SET @idx_cmp = 1
SET @bin_cmp = 0x0000
SET @lev_dist = 0  


IF @str_ref = @str_cmp BEGIN 
	SET @lev_dist = 0 
END 


WHILE @idx_cmp <= @len_cmp BEGIN   
	SET @bin_cmp = @bin_cmp + CAST(@idx_cmp AS BINARY(2))
	SET @idx_cmp = @idx_cmp + 1  
END	     
	
WHILE @idx_ref <= @len_ref   BEGIN    
	SET @chr_ref = SUBSTRING(@str_ref, @idx_ref, 1)
	SET @lev_dist = @idx_ref
	SET @bin_ref = CAST(@idx_ref AS BINARY(2))
	SET @idx_cmp = 1     
			
	WHILE @idx_cmp <= @len_cmp    BEGIN      
		SET @chr_cmp = SUBSTRING(@str_cmp, @idx_cmp, 1)
		SET @lev_dist = @lev_dist + 1      
		SET @idx_tmp = CAST(SUBSTRING(@bin_cmp, (@idx_cmp + @idx_cmp - 1), 2) AS INT) 
				 + CASE WHEN @chr_ref = @chr_cmp THEN 0 ELSE 1 END      
		SET @lev_dist = CASE WHEN @lev_dist > @idx_tmp THEN @idx_tmp ELSE @lev_dist END      
		SET @idx_tmp = CAST(SUBSTRING(@bin_cmp, @idx_cmp + @idx_cmp + 1, 2) AS INT) + 1      
		SET @lev_dist = CASE WHEN @lev_dist > @idx_tmp THEN @idx_tmp ELSE @lev_dist END      		
		SET @bin_ref = @bin_ref + CAST(@lev_dist AS BINARY(2))
		SET @idx_cmp = @idx_cmp + 1    
	END    
			
	SET @bin_cmp = @bin_ref
	SET @idx_ref = @idx_ref + 1  
END   

SELECT 'lev_dist' = @lev_dist
	
END
	
	